#include "line.h"
#include "shape.h"
#include "vertex.h"


void drawline(VERTEX_2I v1, VERTEX_2I v2) {
   int x0 = v1.x, y0=v1.y ,xEnd=v2.x, yEnd=v2.y;
   int dx = abs(xEnd - x0),  dy = abs(yEnd - y0);
   int p = 2 * dy - dx;
   int twoChange = 2 * dy,  twoChangeDiff = 2 * (dy - dx);
   GLint x, y;

   int m,n,o;

   int is_m_neg, is_n_neg;

   int slope_high = 0;

   /* Determine which endpoint to use as start position.  */
   if (x0 > xEnd) {
      x = xEnd;
      y = yEnd;
      xEnd = x0;
      yEnd = y0;
   }
   else {
      x = x0;
      y = y0;
   }
   setPixel (x, y);

   if(dy>dx){
     //slope greater than 1
   m = y;
   n = x;
   o = yEnd;
   p = 2 * dx - dy;

   twoChange = 2 * dx;
   twoChangeDiff = 2 * (dx - dy);

   if(m>o){
     is_m_neg = 1;
   } else {
     is_m_neg = 0;
   }
   is_n_neg = 0;

   slope_high = 1;

   } else {
     //slope less than 1
     m = x;
   n = y;
   o = xEnd;
   p = 2 * dy - dx;
   twoChange = 2 * dy;
   twoChangeDiff = 2 * (dy - dx);

   is_m_neg = 0;

   if(y > yEnd){
     is_n_neg = 1;
   } else {
     is_n_neg = 0;
   }

   slope_high = 0;

   }

   while (m != o) {
    if(is_m_neg == 1){
      m--;
    } else{
      m++;
    }
      if (p < 0)
         p += twoChange;
      else {
      if(is_n_neg == 1){
        n--;
      } else {
        n++;
      }
         p += twoChangeDiff;
      }
    if(slope_high == 1){
      setPixel (n, m);

    } else{
      setPixel (m, n);
    }
   }

}
